﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Data.SqlClient;

public partial class UserControl_Login : System.Web.UI.UserControl
{
    SqlConnection sqlconn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);

    protected void Page_Load(object sender, EventArgs e)
    {
        Panel1.Visible = false;
        if (!IsPostBack)
        {
            if (Session["Username"] != null)
            {
                Panel1.Visible = true;
                Panel2.Visible = false;
                UsernameTextBox.Text = Session["Username"].ToString();
            }
        }
    }
    protected void loginButton_Click(object sender, EventArgs e)
    {
        sqlconn.Open();
        SqlCommand sqlcom = new SqlCommand("loginUserInfo", sqlconn);
        sqlcom.CommandType = CommandType.StoredProcedure;
        sqlcom.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = UsernameTextBox.Text.Trim();
        sqlcom.Parameters.Add("@Password", SqlDbType.VarChar, 50).Value = PasswordTextBox.Text.Trim();
        SqlDataReader read = sqlcom.ExecuteReader();
        if (read.Read())
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
            UsernameLabel.Text = UsernameTextBox.Text;
            Session["Username"] = UsernameTextBox.Text.Trim();
        }
        else
        {
            Response.Write("<script>alert('您输入的用户名或密码错误，请重新输入!');location='javascript:history.go(-1)';</script>");
        }
    }
    protected void registerButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/registrationpage.aspx");
    }
    protected void logoutButton_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false;
        Panel2.Visible = true;
        UsernameTextBox.Text = "";
        PasswordTextBox.Text = "";
        FormsAuthentication.SignOut();//清除验证信息
        HttpContext.Current.Session.Clear();//清除Session内容
        HttpContext.Current.Session.Abandon();//取消当前会话
    }
}
